Ext.define("Portal.controller.main.ToolbarWrapController",{
    extend:'Ext.app.ViewController',
    alias: 'controller.main.toolbarWrapController',
    listen:{
        controller:{
            '#' : {
                unmatchedroute : 'onRouteChange'
            }
        },
        global: {
            'saveCustomMenu':'saveCustomMenu'
        },
        component:{
            "#gridBox":{
                select:'onGridItemSelect'
            }
        }
    },

    //menu
    setActiveMenu:function(id){
        var toolbar = this.getReferences().toolbanrMenu;
        var views = toolbar.items.items;
        views.forEach(function (item, index) {
            var href = item.href,configMenu = item.config.menu;
            item.removeCls('toolbar-btn-active');
            if(href && href.indexOf(id)>=0){
                item.addCls('toolbar-btn-active');
            }
            if(configMenu){
                configMenu.forEach(function (ele,i) {
                    var _href = ele.href;
                    if(_href && _href.indexOf(id)>=0){
                        item.addCls('toolbar-btn-active');
                    }
                })
            }
        });
    },
    onRouteChange:function (id) {
        this.setActiveMenu(id);
    },

    //customMenu
    customMenuChange:function(){
        console.log('12');
    },
    customMenuRender:function(){
        var menu = JSON.parse(Cookies.get('quickNav'));
        this.saveCustomMenu(menu);
    },
    saveCustomMenu:function (menu) {
        menu = menu.concat([
            {xtype: 'menuseparator'},
            {
                text:'自定义设置',
                href:'#setting.customSettings',
                hrefTarget: '_self'
            }
        ]);
        this.view.setMenu(Ext.create('Ext.menu.Menu',{
            width:200,
            plain:true,
            cls:'webEasy-menu-box',
            items:menu
        }))
    },

    //search
    onGridItemSelect:function (view, record, index, eOpts) {
        this.redirectTo(record.get('routeId'));
        this.view.setValue(record.get('name'));
        this.view.collapse();
        this.view.blur();
    },
    searchOnClick:function (view) {
        var toolbanrSearch = this.getReferences().toolbanrSearch;
        if(toolbanrSearch.isExpanded) return;
        toolbanrSearch.onTriggerClick();
        toolbanrSearch.focus();
    },
    searchInput:function (veiw, newValue) {

    },


    //user
    userListClick:function (menu,item) {
        Ext.fireEvent('toolbarClick',item)
    },
    userSignOut:function () {
        Cookies.remove('userName', { path: 'http://'+window.location.host+'/portal/' });
        Cookies.remove('password', { path: 'http://'+window.location.host+'/portal/' });
        Cookies.remove('quickNav', { path: 'http://'+window.location.host+'/portal/' });
        Cookies.remove('roles', { path: 'http://'+window.location.host+'/portal/' });
        window.location.href='http://'+window.location.host+'/portal/login.html';
    }
});